//
//  RWOrderDetailViewController.m
//  RedWine
//
//  Created by Liufangfang on 2017/1/18.
//  Copyright © 2017年 HuiBangKe. All rights reserved.
//

#import "RWOrderDetailViewController.h"
#import "RWOrderDetailHeaderView.h"
#import "RWBalanceGoodsViewCell.h"

#import "RWMyOrderDetailModel.h"
#import "RWAddressModel.h"

#import "FTYInfoVertifyTools.h"

@interface RWOrderDetailViewController ()

@property (nonatomic, copy) RWMyOrderDetailModel *myOrderDetailModel;
@property (nonatomic, copy) RWAddressModel *addressModel;
@property (nonatomic, copy) NSMutableArray *goods;
//发票信息
@property (nonatomic, copy) NSDictionary *invoiceInfo;

@end

@implementation RWOrderDetailViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initObjects];
    [self loadDatas];
    [self setNavigation];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Load Datas

- (void)initObjects{
   
    _myOrderDetailModel = [[RWMyOrderDetailModel alloc] init];
    _addressModel = [[RWAddressModel alloc] init];
    _goods = [NSMutableArray array];
    _invoiceInfo = [[NSDictionary alloc] init];
}

- (void)loadDatas{
 
    FTYUser *user = [FTYUser sharedUser];
    [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=order&member_id=%@&order_sn=%@", user.userID, _orderSN] parameters:nil block:^(id result, NSError *error) {
        
        if (!error) {
            
            DLog(@"订单详情====%@", result);
            FTYBaseShopCartModel *baseModel = [[FTYBaseShopCartModel alloc] init];
            baseModel = [FTYBaseShopCartModel mj_objectWithKeyValues:result];
            FTYErrorInfoModel *errorInfo = [FTYErrorInfoModel mj_objectWithKeyValues:baseModel.err];
            
            if (0 == errorInfo.errorcode) {
                
                _myOrderDetailModel = [RWMyOrderDetailModel mj_objectWithKeyValues:baseModel.info];
                _addressModel = [RWAddressModel mj_objectWithKeyValues:_myOrderDetailModel.addr];
                [_goods addObjectsFromArray:_myOrderDetailModel.goods];
                _invoiceInfo = _myOrderDetailModel.invoice_info;
                
                [self setOrderDetailUI];
            } else {
                [[FTYToastView alloc] addTitle:errorInfo.errorinfo addView:self.view];
            }
        } else {
            
        }
    }];
}

#pragma mark - Config UI

- (void)setNavigation{
    
    self.title = @"订单详情";
}

- (void)setOrderDetailUI{
    
    _orderDetailTV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, FTY_SCREEN_HEIGHT- FTY_TABBAR_HEIGHT) style:UITableViewStylePlain];
    [self.view addSubview:_orderDetailTV];
    _orderDetailTV.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    _orderDetailTV.separatorStyle = UITableViewCellSeparatorStyleNone;
    _orderDetailTV.dataSource = self;
    _orderDetailTV.delegate = self;
    _orderDetailTV.tableHeaderView = [self createHeader];
    _orderDetailTV.tableFooterView = [self createFooter];
}

#pragma mark - Config Invoke Methods

- (UIView *)createHeader{
    
    RWOrderDetailHeaderView *headerV = [[RWOrderDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, 355.*FTY_HEIGHT_BASE)];
    [headerV loadDataWithOrderModel:_myOrderDetailModel andAddressModel:_addressModel];
    return headerV;
}

- (UIView *)createFooter{

    UIView *footerV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, FTY_SCREEN_WIDTH, 100.*FTY_HEIGHT_BASE)];
    
    UILabel *billFlagLab = [[UILabel alloc] init];
    [footerV addSubview:billFlagLab];
    billFlagLab.text = @"发票信息";
    billFlagLab.textColor = [FTYColor blackColor];
    billFlagLab.font = FTY_FONTTSIZE(15);
    [billFlagLab sizeToFit];
    [billFlagLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(20.*FTY_WIDTH_BASE);
        make.left.mas_offset(10.*FTY_WIDTH_BASE);
    }];
    
    NSInteger type = [[_invoiceInfo objectForKey:@"type"] integerValue];
    NSString *category = @"没有发票";
    switch (type) {
        case 0:
            category = @"没有发票";
            break;
        case 1:
            category = @"普通发票";
            break;
        case 2:
            category = @"增值税发票";
            break;
            
        default:
            break;
    }
    
    UILabel *billLab = [[UILabel alloc] init];
    [footerV addSubview:billLab];
    billLab.text = category;
    billLab.textColor = [FTYColor blackColor];
    billLab.font = FTY_FONTTSIZE(15);
    [billLab sizeToFit];
    [billLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(billFlagLab.mas_top);
        make.right.mas_offset(-10.*FTY_WIDTH_BASE);
    }];
    
    NSArray *billNames = @[@"单位名称：", @"发票内容："];
    NSArray *billContents = [NSArray array];
    NSString *taiTou = [_invoiceInfo objectForKey:@"taitou"];
    NSString *content = [_invoiceInfo objectForKey:@"neirong"];
    if ([FTYInfoVertifyTools isnilVertify:taiTou]) {
        taiTou = @"无";
    } else {
        taiTou = [_invoiceInfo objectForKey:@"taitou"];
    }
    if ([FTYInfoVertifyTools isnilVertify:content]) {
        content = @"无";
    } else {
        content = [_invoiceInfo objectForKey:@"neirong"];
    }
    
    billContents = @[taiTou, content];
    
    for (int i=0; i<2; i++) {
        
        UILabel *billNameLab = [[UILabel alloc] init];
        [footerV addSubview:billNameLab];
        billNameLab.text = billNames[i];
        billNameLab.textColor = [FTYColor blackColor];
        billNameLab.font = FTY_FONTTSIZE(15);
        [billNameLab sizeToFit];
        [billNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(billFlagLab.mas_bottom).offset((10.+30.*i)*FTY_HEIGHT_BASE);
            make.left.mas_offset(10.*FTY_WIDTH_BASE);
        }];
        
        UILabel *billContentLab = [[UILabel alloc] init];
        [footerV addSubview:billContentLab];
        billContentLab.text = billContents[i];
        billContentLab.textColor = [FTYColor blackColor];
        billContentLab.font = FTY_FONTTSIZE(15);
        [billContentLab sizeToFit];
        [billContentLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(billNameLab.mas_top);
            make.left.mas_equalTo(billNameLab.mas_right).offset((10.)*FTY_WIDTH_BASE);
        }];
    }
    
    return footerV;
}

#pragma mark - Notification Method

#pragma mark - Notification Invoke Methods

#pragma mark - <UITableViewDataSource>

#pragma mark - <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _goods.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 10.*FTY_HEIGHT_BASE;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 120.*FTY_HEIGHT_BASE;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * const orderDetailID = @"orderListID";
    //与关注公用一个cell
    RWBalanceGoodsViewCell *orderListCell = [tableView dequeueReusableCellWithIdentifier:orderDetailID];
    
    if (!orderListCell) {
        
        orderListCell = [[RWBalanceGoodsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:orderDetailID];
        orderListCell.selectionStyle = UITableViewCellSeparatorStyleNone;
    }
    [orderListCell loadDataWithMyOrderDetailModel:_goods[indexPath.row]];
    
    return orderListCell;
}

#pragma mark - <UITableViewDelegate>

#pragma mark - <UITableViewDelegate>

#pragma mark - Private Invoke Methods

@end
